<template>
  <div class="login">
    <el-form :model="form" label-width="80px">
      <el-form-item label="手机号">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-button type="primary" @click="login">登录</el-button>
      <el-button @click="$router.push('/register')">注册</el-button>
    </el-form>
  </div>
</template>

<script setup>
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
import { useAuthStore } from '../stores/auth';

const router = useRouter();
const authStore = useAuthStore();
const form = reactive({ phone: '', password: '' });

const login = async () => {
  try {
    const response = await axios.post('/api/login', form);
    authStore.setToken(response.data.token, response.data.user);
    router.push('/explore');
  } catch (error) {
    alert('登录失败：' + (error.response?.data?.error || '未知错误'));
  }
};
</script>

<style scoped>
.login { max-width: 400px; margin: 50px auto; }
</style>